package com.lagou.module01.servlet;

import com.lagou.module01.entity.Student;
import com.lagou.module01.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "StudentAddServlet", urlPatterns = "/studentAdd")
public class StudentAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符编码为UTF-8
        request.setCharacterEncoding("utf-8");

        // 设置编码方式，解决中文乱码的问题
        response.setContentType("text/html; charset=utf-8");

        //获取前台页面发过来的标志位和数据内容
        String flag = request.getParameter("flag");

        //获取参数保存在Student对象中
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String remark = request.getParameter("remark");

        //获取StudentService类型的对象
        StudentService studentService = new StudentService();
        // 根据隐藏域的结果分别实现增加和修改的功能
        int res = 0;
        Student student = null;
        if ("add".equals(flag)) {
            student = new Student(name, sex, birthday, email, remark);
            res = studentService.StudentAdd(student);
            //System.out.println("插入数据的结果为：" + res);
        } else {
            student = new Student(Integer.parseInt(flag), name, sex, birthday, email, remark);
            res = studentService.StudentUpdate(student);
            System.out.println("修改数据的结果为：" + res);
        }

        // 3.通过输出流向前台页面展示处理结果，然后重新请求一次数据
        PrintWriter writer = response.getWriter();
        // 获取模块名称
        String contextPath = this.getServletContext().getContextPath();
        // 拼接请求路径
        String hrefPath = contextPath + "/findUserByPage";
        if(0 != res) {
            writer.print("<script>alert('管理学生信息成功！'); location.href='" + hrefPath
                    + "';</script>");
        }else{
            writer.print("<script>alert('管理学生信息失败！'); location.href='" + hrefPath
                    + "';</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
